/*
 * LDIR
 *
 * LDIR, a GPS cartography application built for Let's Do It Romania eco
 * campaign.
 * Copyright (C) 2010 Genera Software <office@generasoftware.com>
 *
 * http://generasoftware.com/
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
#include "gpsnavidecorator.h"
#include <aknutils.h>
#include <aknsutils.h>
#include <eiklabel.h>

const TInt KTimeOut = 100000; // 100 ms
const TInt KSquaresNo = 6;
const TInt KSquareGap = 2; // percents

CGPSNaviDecorator* CGPSNaviDecorator::NewL()
    {
    CGPSNaviDecorator* me = CGPSNaviDecorator::NewLC();
    CleanupStack::Pop( me );
    return me;
    }

CGPSNaviDecorator* CGPSNaviDecorator::NewLC()
    {
    CGPSNaviDecorator* me = new ( ELeave ) CGPSNaviDecorator;
    CleanupStack::PushL( me );
    me->ConstructL();
    return me;
    }

CGPSNaviDecorator::~CGPSNaviDecorator()
    {
    delete iSearchingTimer;
    }

void CGPSNaviDecorator::HandleResourceChange( TInt aType )
    {

    }

void CGPSNaviDecorator::SetSatInUseL( TInt aSatInUse )
    {
    if ( aSatInUse > 1 )
        {
        iState = EStatePositioning;
        iSearchingTimer->Cancel();
        iSatInUse = aSatInUse;
        DrawDeferred();
        }
    }

void CGPSNaviDecorator::SetSearchingL()
    {
    iState = EStateSearching;
    iSearchingTimer->Start( 0, KTimeOut, TCallBack( TimerCallback, this ) );
    }

TInt CGPSNaviDecorator::TimerCallback( TAny* aPtr )
    {
    CGPSNaviDecorator* me = static_cast<CGPSNaviDecorator*> ( aPtr );
    me->TimerHandler();
    return 0;
    }

void CGPSNaviDecorator::SizeChanged()
    {

    }

void CGPSNaviDecorator::Draw( const TRect& aRect ) const
    {
    DrawBackground( aRect );
    TInt indicatorWidth = Rect().Width() - 8;
    TInt rectHeight = Rect().Height();
    TInt squareGap = ( indicatorWidth * KSquareGap ) / 100;
    TInt squareWidth = ( indicatorWidth - ( KSquaresNo + 2 ) * squareGap )
        / KSquaresNo;
    TSize squareSize( squareWidth, rectHeight - 2 * squareGap );
    TRgb color;
    MAknsSkinInstance* skin = AknsUtils::SkinInstance();
    AknsUtils::GetCachedColor( skin, color, KAknsIIDQsnTextColors,
        EAknsCIQsnTextColorsCG6 );

    CWindowGc& gc = SystemGc();
    gc.SetBrushColor( color );
    gc.SetPenColor( color );

    if ( EStateSearching == iState )
        {
        for ( TInt i = 0; i < KSquaresNo; ++i )
            {
            if ( iPos == i )
                {
                gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
                }
            TPoint position( i * ( squareWidth + squareGap ), squareGap );
            gc.DrawEllipse( TRect( Position() + position, squareSize ) );
            gc.SetBrushStyle( CGraphicsContext::ENullBrush );
            }
        }
    else
        {
        for ( TInt i = 0; i < KSquaresNo; ++i )
            {
            if ( iSatInUse > i )
                {
                gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
                }
            TPoint position( i * ( squareWidth + squareGap ), squareGap );
            gc.DrawEllipse( TRect( Position() + position, squareSize ) );
            gc.SetBrushStyle( CGraphicsContext::ENullBrush );
            }
        }
    }

void CGPSNaviDecorator::ConstructL()
    {
    iSearchingTimer = CPeriodic::NewL( CActive::EPriorityStandard );
    }

void CGPSNaviDecorator::TimerHandler()
    {
    DrawDeferred();
    if ( !iDirection )
        {
        ++iPos;
        }
    else
        {
        --iPos;
        }
    if ( !iDirection && iPos == ( KSquaresNo - 1 ) )
        {
        iDirection = !iDirection;
        }
    if ( iDirection && iPos == 0 )
        {
        iDirection = !iDirection;
        }
    }

